(function(){
var ta = {};
$.action = function(){
	if (arguments.length == 0) return;
	if (typeof arguments[0] == 'string' && arguments[0].indexOf('.') != -1) {
		var t = arguments[0].split('.');
		if (t.length == 2) {
			var args = [];
			for(var i = 1,len = arguments.length; i < len; i++) args.push(arguments[i]);
			var target = $.action[t[0]];
			if (!target) {
				alert("不存在这个方法：" + arguments[0]);
				return false;
			}
			var lt = typeof ta[t] == 'undefined' ? 0 : ta[t];
			var nt = new Date().getTime();
			if (nt - lt < 1000) {
				ta[t] = nt;
				return;
			}
			var data = target[t[1]].apply(this, args);
			if (data == null) return false;
			if (typeof data == 'string') {
				alert(data);
				return false;
			}
			if (typeof data == 'object') {
				$.post('/action/' + t[0] + '_' + t[1],data,
					function(ret){
						if (typeof target[t[1] + '_cb'] == 'function') typeof target[t[1] + '_cb'].apply(null,[ret].concat(args));
					},'text');
			}
		}
	}	
	return false;
};
$.action.remark = {
	add:function(form) {
		var content = $.trim(form.content.value);
		if (!content) {
			alert('内容不能为空');
			form.content.focus();
			return;
		}
		content = content.replace(/\n/ig, '<br/>').replace(/ /g, '&nbsp;');
		return {
			recipeId:form.recipeId.value,
			foodId:form.foodId.value,
			content:content
		};
	},
	add_cb:function(ret,form){
		if ($('#remarkList li').length == 0) {
			$('#remarkList').append(ret);
		} else {
			$('#remarkList li:first').before(ret);
		}
		form.content.value = '';
	}
};
$.action.fav = {
	add:function(recipeId,sender) {
		return {recipeId:recipeId};
	},
	add_cb:function(ret,form,sender) {
		sender.value = "取消收藏";
	},
	cancel:function(recipeId,sender){
		return {recipeId:recipeId};
	},
	cancel_cb:function(ret,recipeId,sender) {
		sender.value = "收藏";
	}
};
$.action.email = {
	exist:function(form) {
		var email = $.trim(form.email.value);
		if (!email) {
			form.email.focus();
		} else if (!/^\w+([-+.']\w+)*@\w+([-.]\w+)*(\.\w+([-.]\w+)*)*$/.test(email)) {
			alert("邮箱格式不正确");
			form.email.select();
		} else return {email:email};
	},
	exist_cb:function(ret,form){
		if (ret == "1") {
			alert("该邮箱已经注册过了");
			form.email.select();
		} else if (ret == "0"){
			alert("该邮箱还没有注册，可以继续使用");
			form.nickname.focus();
		} else {
			alert(ret);
		}
	}
};
})()